package com.matriq.cloudmanager;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.LeaveDetail;
import com.matriq.cloudmanager.entity.LeaveMsg;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.ExceptionAsyncTask;
import net.lgpage.util.Cleaner;

public class LevelDetailActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_level_detail);
		setViews();
		setEvents();
		loadData();
	}

	TextView tv[] = new TextView[8];
	TextView tv_title;
	ImageButton ll_back;

	private void setViews() {
		tv[0] = (TextView) this.findViewById(R.id.tv0);
		tv[1] = (TextView) this.findViewById(R.id.tv1);
		tv[2] = (TextView) this.findViewById(R.id.tv2);
		tv[3] = (TextView) this.findViewById(R.id.tv3);
		tv[4] = (TextView) this.findViewById(R.id.tv4);
		tv[5] = (TextView) this.findViewById(R.id.tv5);
		tv[6] = (TextView) this.findViewById(R.id.tv6);
		tv[7] = (TextView) this.findViewById(R.id.tv7);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
	}

	private void setEvents() {
		
	}

	LeaveMsg obj;

	LeaveDetail detail;
	public void setData() {
		if(obj!=null){
			tv[0].setText(obj.getBtime() + " 至 " + obj.getEtime());
			tv[1].setText(obj.getType());
			tv[3].setText(obj.getState());
			tv[4].setText(obj.getCause());
			tv[7].setText(obj.getMark());
		}
		if(detail!=null){
			tv[0].setText(obj.getBtime() + " 至 " + obj.getEtime());
			tv[1].setText(detail.getType());
			tv[3].setText(detail.getStatu());
			tv[2].setText(detail.getTime()+"分钟");
			tv[4].setText(detail.getReason());
			tv[5].setText(detail.getWho());
			tv[6].setText(detail.getMark());
			tv[7].setText(detail.getFtime());
		}
	}

	String id;
	private void loadData() {
		id = getIntent().getStringExtra("id");
		if(id == null){
			obj = (LeaveMsg) intentGetObj(LeaveMsg.class);
			id = obj.getNum();
		}
		setData();
		reloadObj();
	}
	ObjTask objTask;

	public void reloadObj() {
		if (objTask != null) {
			objTask.cancel(true);
		}
		objTask = new ObjTask(getActivity());
		objTask.execute();
	}

	private class ObjTask extends ExceptionAsyncTask<LeaveDetail> {

		public ObjTask(Context context) {
			super(context);
		}

		@Override
		public LeaveDetail doInBackgroundX(String... params) throws Exception {
			return AccountService.getService().ask_info(id);
		}

		@Override
		public void onPostExecuteX(LeaveDetail result) {
			if (result == null) {
				return;
			} else {
				detail = result;
				setData();
			}
		}
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(objTask);
		super.onDestroy();
	}

}